const QQMapWX = require('./qqmap-wx-jssdk.min.js')

const qqmapsdk = new QQMapWX({
  key: '3M3BZ-OAKKD-4DS42-PICU5-AVGO6-H6BNN' // 必填
})

export const handleAddress = data => {
  return new Promise(resolve => {
    qqmapsdk.geocoder({
      // 获取表单传入地址
      address: data.address, // 地址参数，例：固定地址，address: '北京市海淀区彩和坊路海淀西大街74号'
      success: function (ress) { // 成功后的回调
        console.log(ress)
        var res = ress.result
        var latitude = res.location.lat
        var longitude = res.location.lng
        // 根据地址解析在地图上标记解析地址位置
        let addressBean = {
          addressProvince: res.address_components.province,
          addressArea: res.address_components.district,
          addressCity: res.address_components.city,
          addressDetail: data.address,
          addressSimple: data.name,
          latitude,
          longitude
        }
        resolve(addressBean)
      },
      fail: function (error) {
        console.error(error)
        resolve({})
      },
      complete: function (res) {
        console.log(res)
        resolve({})
      }
    })
  })
}
